package com.quan.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quan.commons.core.biz.support.MyBaseServiceImpl;
import com.quan.commons.core.utils.PageUtils;
import com.quan.mall.entity.PmsSku;
import com.quan.mall.entity.WmsAgentInfo;
import com.quan.mall.service.PmsSkuService;
import com.quan.mall.service.WmsAgentInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.quan.mall.commons.vo.WmsWareSkuVo;
import com.quan.mall.entity.WmsWareSku;
import com.quan.mall.mapper.WmsWareSkuMapper;
import com.quan.mall.service.WmsWareSkuService;


@Slf4j
@Service
public class WmsWareSkuServiceImpl extends MyBaseServiceImpl<WmsWareSkuMapper, WmsWareSku> implements WmsWareSkuService {

    @Autowired
	private WmsWareSkuMapper mapper;

    @Autowired
	private WmsAgentInfoService wmsAgentInfoService;

    @Autowired
	private PmsSkuService pmsSkuService;

	@Override
    public List<WmsWareSkuVo> queryList(PageUtils pageUtils, WmsWareSkuVo vo) {
        QueryWrapper<WmsWareSku> queryWrapper = new QueryWrapper<WmsWareSku>(vo);

        List<WmsWareSku> list = super.list(pageUtils, queryWrapper);
        List<WmsWareSkuVo> collect = list.stream().map(item -> {
            WmsWareSkuVo bean = new WmsWareSkuVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public PageUtils queryPage(PageUtils pageUtils, WmsWareSkuVo vo) {
        QueryWrapper<WmsWareSku> queryWrapper = new QueryWrapper<WmsWareSku>(vo);

        if(StringUtils.isNotBlank(vo.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("name", vo.getKeyword());
            });
        }

        final IPage<WmsWareSku> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);

        final List<WmsWareSku> records = page.getRecords();

        final List<WmsWareSku> collect = records.stream().map((item) -> {
            WmsWareSkuVo bean = new WmsWareSkuVo();
            BeanUtils.copyProperties(item, bean);

            final WmsAgentInfo info = this.wmsAgentInfoService.getById(item.getAgentId());
            if (info != null) {
                bean.setAgentName(info.getCompanyName());
            }

            return bean;
        }).collect(Collectors.toList());

        ps.setRecords(collect);
        return ps;
    }

    @Override
    public void batchStock(List<Long> ids, Integer stock) {
        UpdateWrapper<WmsWareSku> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("stock", stock);
        updateWrapper.in("id", ids);
        super.update(updateWrapper);
    }

//    @Override
//    public void decrCount(Map<Long, Integer> decrMap) {
//        for (Map.Entry<Long, Integer> entry : decrMap.entrySet()) {
//            // 商品id
//            Long skuId = Long.valueOf(entry.getKey());
//            // 递减数量
//            Object value = entry.getValue();
//            Integer num = Integer.valueOf(value.toString());
//
//            // 库存 >= 递减数量
//            WmsWareSku sku = super.getById(skuId);
//            if (sku.getStock() >= num) {
//                this.baseMapper.decrCount(skuId, num);
//            }
//        }
//    }

    @Override
    public Integer getAgentSkuStock(Long agentId, Long skuId) {
        QueryWrapper<WmsWareSku> queryWrapper = new QueryWrapper<WmsWareSku>();
        queryWrapper.eq("agent_id", agentId);
        queryWrapper.eq("sku_id", skuId);

        final WmsWareSku wareSku = super.getOne(queryWrapper);
        // 总库存-出库=剩余库存
        return wareSku.getStock() - wareSku.getStockOut();
    }

    @Override
    public void getStockIncrease(Long agentId, Long skuId, Integer stockOutNum) {
        QueryWrapper<WmsWareSku> queryWrapper = new QueryWrapper<WmsWareSku>();
        queryWrapper.eq("agent_id", agentId);
        queryWrapper.eq("sku_id", skuId);

        final WmsWareSku wareSku = super.getOne(queryWrapper);
        wareSku.setStockOut(wareSku.getStockOut()+1);
        super.updateById(wareSku);
    }

    @Override
    public void getStockDecrease(Long agentId, Long skuId, Integer stockOutNum) {
        QueryWrapper<WmsWareSku> queryWrapper = new QueryWrapper<WmsWareSku>();
        queryWrapper.eq("agent_id", agentId);
        queryWrapper.eq("sku_id", skuId);

        final WmsWareSku wareSku = super.getOne(queryWrapper);
        wareSku.setStockOut(wareSku.getStockOut()-1);
        super.updateById(wareSku);

    }

    @Override
    public void refreeSkus(List<Long> agentIds, List<Long> skuIds) {
        for (Long agentId : agentIds) {

            for (Long skuId : skuIds) {
                QueryWrapper<WmsWareSku> queryWrapper = new QueryWrapper<WmsWareSku>();
                queryWrapper.eq("agent_id", agentId);
                queryWrapper.eq("sku_id", skuId);
                final WmsWareSku wareSku = super.getOne(queryWrapper);
                if(null == wareSku) {
                    final PmsSku sku = this.pmsSkuService.getById(skuId);
                    WmsWareSku ws = new WmsWareSku();
                    ws.setAgentId(agentId);
                    ws.setSkuId(skuId);
                    ws.setSkuName(sku.getSkuName());
                    ws.setSkuAttr(sku.getDescar());
                    ws.setStock(0);
                    super.save(ws);
                }
            }


        }
    }
}